package com.guinea.core.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.guinea.core.orm.Page;
import com.guinea.core.utils.StringUtils;

public class PageTag extends TagSupport {

	private static final long serialVersionUID = -774840953985543612L;
	
	String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int doEndTag() throws JspException{
		Page page = (Page)pageContext.getRequest().getAttribute(name);
		String html = "";
		if(page != null){
			html += "<input type=\"hidden\" name=\"page_pageNo\" class=\"pageNo\" value=\""+page.getPageNo()+"\" />";
			html += "<input type=\"hidden\" name=\"page_orderBy\" class=\"orderBy\" value=\""+StringUtils.trimToEmpty(page.getOrderBy())+"\" />";
			html += "<input type=\"hidden\" name=\"page_order\" class=\"order\" value=\""+StringUtils.trimToEmpty(page.getOrder())+"\" />";
			html += "<table class=\"page\">";
			html += "	<tr>";
			html += "		<td class=\"page-message\">";
			html += "			<span>第 "+page.getPageNo()+" 页, 共 "+page.getTotalPages()+" 页,"+page.getTotalCount()+" 条记录。</span>";
			html += "		</td>";
			html += "		<td class=\"page-operation\">";
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage(1,this)\">首页</span>";
			if(page.isHasPre())
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getPrePage()+",this)\">上一页</span>";
			if(page.isHasNext())
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getNextPage()+",this)\">下一页</span>";
			html += "			<span class=\"link\" onclick=\"javascript:jumpPage("+page.getTotalPages()+",this)\">末页</span>";
			html += "		</td>";
			html += "	</tr>";
			html += "</table>";
		}else {
			html += "<input type=\"hidden\" name=\"page_pageNo\" class=\"pageNo\" value=\"1\" />";
			html += "<input type=\"hidden\" name=\"page_orderBy\" class=\"orderBy\" value=\"\" />";
			html += "<input type=\"hidden\" name=\"page_order\" class=\"order\" value=\"\" />";
		}
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
}
